package com.eight.cloud.mybatisplus.datascope;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eight.cloud.core.global.model.DataScopeModel;
import com.eight.cloud.core.global.model.BaseUser;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Column;

import static com.eight.cloud.core.global.constant.StrConstant.DOT;

/**
 * @InterfaceName: SqlWhereExpression
 * @Author: TXC
 * @Date: 2025-06-11 15:48
 **/
public interface SqlWhereExpression {

    default Expression getExpression(String tableAlias, String columnName, BaseUser currentUser, DataScopeModel dataScopeModel) {
        if (isSupport(dataScopeModel) && ObjectUtil.isNotNull(currentUser)) {
            return getExpression(buildColumn(tableAlias, columnName), currentUser, dataScopeModel);
        }
        return null;
    }

    Expression getExpression(Column column, BaseUser currentUser, DataScopeModel dataScopeModel);
    /**
     * 构建Column
     *
     * @param tableAlias 表别名
     * @param columnName 字段名称
     * @return 带表别名字段
     */
    default Column buildColumn(String tableAlias, String columnName) {
        if (StrUtil.isNotBlank(tableAlias)) {
            columnName = tableAlias + DOT + columnName;
        }
        return new Column(columnName);
    }

    boolean isSupport(DataScopeModel dataScopeModel);
}
